package com.hot100.mid;

/**
 * @author zhengTao
 * @version 1.0
 * @description: 除自身以外数组的乘积
 * @date 2022/10/1 18:40
 */
public class Demo43 {
    public static void main(String[] args) {
         int[] nums=new int[]{1,2,3,4};
          productExceptSelf(nums);
        for (int num : nums) {
            System.out.println(num);
        }
    }

    public static int[] productExceptSelf(int[] nums) {
        if(nums.length<=1){
            return nums;
        }
        int[] left=new int[nums.length];
        int[] right=new int[nums.length];
        int leftSum=1;
        left[0]=1;
        right[right.length-1]=1;
        int rightSum=1;
        for (int i = 1; i < left.length; i++) {
            leftSum=leftSum*nums[i-1];
            left[i]=leftSum;
        }
        for (int length = right.length-1; length >0; length--) {
            rightSum=rightSum*nums[length];
            right[length-1]=rightSum;
        }
        for (int i = 0; i < nums.length; i++) {
            nums[i]=left[i]*right[i];
        }
        return nums;
    }
}
